﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TANKWAR_alpha
{
    class TankRobot:ObjElement
    {
        public TankRobot(int left,int top,GameGDI _gameGDI)
        {
            this.Left = left;
            this.Top = top;
            this.Width = Constant.tankWidth;
            this.Height = Constant.tankHeight;
            this.Type = 0;       //RobotTank for 0,1 for robotBoss
            this.Life = true;
            this.Speed = Constant.TankRobotSpeed;
            this.Direction = MoveDirection.UP;
            this.GameDriver = _gameGDI;
        }

        public void MoveByRobotTank()
        {
            int d;
            switch(Direction)
            {
                case MoveDirection.UP: 
                    if (!HitBoundaryOrWallOrtank(this.Left, this.Top - this.Speed, Left, Top))
                        this.Top -= this.Speed;
                    else 
                    { 
                        while((d=GameDriver.D.Next(0,4))!=0)
                        {
                            this.Direction = (MoveDirection)d;
                        }
                    } break;
                case MoveDirection.DOWN: 
                    if (!HitBoundaryOrWallOrtank(this.Left, this.Top + this.Speed,Left,Top ))
                        this.Top += this.Speed; 
                    else
                    {
                        while ((d = GameDriver.D.Next(0, 4)) != 1)
                        {
                            this.Direction = (MoveDirection)d;
                        }
                    } break;
                case MoveDirection.LEFT: 
                    if (!HitBoundaryOrWallOrtank(this.Left - this.Speed, this.Top, Left, Top))
                    this.Left -= this.Speed;
                    else
                    {
                        while ((d = GameDriver.D.Next(0, 4)) != 2)
                        {
                            this.Direction = (MoveDirection)d;
                        }
                    }break;
                case MoveDirection.RIGHT: 
                    if (!HitBoundaryOrWallOrtank(this.Left + this.Speed, this.Top, Left, Top))
                        this.Left += this.Speed; 
                    else
                    {
                        while ((d = GameDriver.D.Next(0, 4)) != 3)
                        {
                            this.Direction = (MoveDirection)d;
                        }
                    }break;

            }

            /*
             * change the direction of RobotTank randomly
             */
            //if(this.GameDriver.D.Next(1,30)==2)
            //{
            //    switch(this.GameDriver.D.Next(0,4))
            //    {
            //        case 0: if (Direction != MoveDirection.UP) Direction = MoveDirection.UP; break;
            //        case 1: if (Direction != MoveDirection.DOWN) Direction = MoveDirection.DOWN; break;
            //        case 2: if (Direction != MoveDirection.LEFT) Direction = MoveDirection.LEFT; break;
            //        case 3: if (Direction != MoveDirection.RIGHT) Direction = MoveDirection.RIGHT; break;
            //        default: break;
            //    }
            //}

        }

        private bool HitBoundaryOrWallOrtank(double left, double top,double preLeft,double preTop)
        {
            /**********jugde whether cross the boundary*************/
            if (left < 0 || left + Constant.tankWidth > Constant.clientWidth || top < 0 || top + Constant.tankHeight > Constant.clientHeight)
                return true;

            /************judge whether making a collision with walls or stells**************/
            foreach (Wall wa in this.GameDriver.Walls)
            {
                    if (wa.WallType != 2&& wa.WallType != 4)
                    {
                   
                            if (Constant.RectangleIntersection(wa.WallLeft, wa.WallTop, Constant.WallWidth, Constant.WallHeight, left, top, Constant.tankWidth, Constant.tankHeight))
                            {
                                return true;
                            }
                    }
                   else if(wa.WallType!=2)
                   {
                            if (Constant.RectangleIntersection(wa.WallLeft, wa.WallTop, Constant.BaseSize, Constant.BaseSize, left, top, Constant.tankWidth, Constant.tankHeight))
                            {
                                return true;
                            }
                   }
              }
            

            /**********judge whether make a crash with RobotTanks**************/
            foreach (TankRobot ta in this.GameDriver.TankRobots)
            {
                    if (ta.Left == preLeft && ta.Top == preTop)
                        continue;
                
                    if (Constant.RectangleIntersection(ta.Left, ta.Top, Constant.tankWidth, Constant.tankHeight, left, top, Constant.tankWidth, Constant.tankHeight))
                    {
                        return true;
                    }
              
            }

            /*********judge whether make a crash with playertank****/
            if (Constant.RectangleIntersection(this.GameDriver.MyTank.Left, this.GameDriver.MyTank.Top, Constant.tankWidth, Constant.tankHeight, left, top, Constant.tankWidth, Constant.tankHeight))
                return true;

            return false;

        }

        public void ShootByRobot()
        {
            Bullet b;
            switch (this.Direction)        //adjust the location and direction of bullet
            {
                case MoveDirection.UP: b = new Bullet(this.Left + Constant.tankWidth / 2.0 - Constant.bombWidth / 2.0, Top - Constant.bombHeight, 0, MoveDirection.UP, this.GameDriver); break;
                case MoveDirection.DOWN: b = new Bullet(this.Left + Constant.tankWidth / 2.0 - Constant.bombWidth / 2.0, Top + Constant.tankHeight + Constant.bombHeight, 0, MoveDirection.DOWN, this.GameDriver); break;
                case MoveDirection.LEFT: b = new Bullet(this.Left - Constant.bombWidth, this.Top + Constant.tankWidth / 2.0 - Constant.bombWidth / 2.0, 0, MoveDirection.LEFT, this.GameDriver); break;
                case MoveDirection.RIGHT: b = new Bullet(this.Left + Constant.tankWidth + Constant.bombWidth, this.Top + Constant.tankWidth / 2.0 - Constant.bombWidth / 2.0, 0, MoveDirection.RIGHT, this.GameDriver); break;
                default: b = null; break;        //can't happen

            }
            this.GameDriver.RobotBullets.Add(b);

        }

        /*
         * Artificial Intelligence for that RobotTank can wisely change their direction
         *    and shoot PlayerTank when they are in the same straight line
         */
        
        public void AI()
        {
            if(this.Top<this.GameDriver.MyTank.Top+Constant .tankHeight&&this.Top>this.GameDriver.MyTank.Top-Constant .tankHeight/2)
            {
                if (this.Left < this.GameDriver.MyTank.Left) this.Direction = MoveDirection.RIGHT;
                else this.Direction = MoveDirection.LEFT;
            }

            if(this.Left>this.GameDriver .MyTank.Left-Constant .tankWidth/2&&this.Left<this.GameDriver.MyTank.Left+Constant .tankWidth)
            {
                if (this.Top < this.GameDriver.MyTank.Top)
                    this.Direction = MoveDirection.DOWN;
                else this.Direction = MoveDirection.UP;
            }

            /*
             * There is another AI method that RobotTanks can detect PlayerBullets locating their next step, that is to say, they can escape the attack like the natural human.But this algorithm remains to be implemented
             * 
             */ 
        }


        internal void ShootByRobotBoss()
        {
            Bullet b;
            b = new Bullet(this.Left + Constant.tankWidth / 2.0 - Constant.bombWidth / 2.0, Top - Constant.bombHeight, 0, MoveDirection.UP, this.GameDriver);
            this.GameDriver.RobotBullets.Add(b);
            b = new Bullet(this.Left + Constant.tankWidth / 2.0 - Constant.bombWidth / 2.0, Top + Constant.tankHeight + Constant.bombHeight, 0, MoveDirection.DOWN, this.GameDriver);
            this.GameDriver.RobotBullets.Add(b);
            b = new Bullet(this.Left - Constant.bombWidth, this.Top + Constant.tankWidth / 2.0 - Constant.bombWidth / 2.0, 0, MoveDirection.LEFT, this.GameDriver);
            this.GameDriver.RobotBullets.Add(b);
            b = new Bullet(this.Left + Constant.tankWidth + Constant.bombWidth, this.Top + Constant.tankWidth / 2.0 - Constant.bombWidth / 2.0, 0, MoveDirection.RIGHT, this.GameDriver);
            this.GameDriver.RobotBullets.Add(b);
                
        }
    }
}
